package com.control;

import com.dao.UserDao;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author :wn
 * @program : 项目五登录界面
 * @descript : 注册用户类
 * @create :2020-10-29 10:39
 */
@WebServlet(value = "/logon")
public class RegisteredUser extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String addname=new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
       String addpassword=new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
       String confirmPassword=new String (req.getParameter("determinepassword").getBytes("ISO-8859-1"),"UTF-8");

        boolean whetherToRegister = false;
        try {
            whetherToRegister = new UserDao().addUser(addname, addpassword, confirmPassword);
            if(whetherToRegister){
                String information="恭喜注册成功！！";
                req.setAttribute("error",information);
                req.getRequestDispatcher("/logon.jsp").forward(req,resp);
            }else {
                String information="注册失败！！";
                req.setAttribute("error",information);
                req.getRequestDispatcher("/logon.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }

    }
}
